package com.squirrel.michale.sort;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/1/18 12:15 PM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class BubbleSort {

    public static void bubbleSort(int[] arr) {

        if (arr == null || arr.length < 2) {
            return;
        }


        for (int i = arr.length-1; i > 0; i--) {

            boolean swapData = false;
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                    swapData = true;
                }
            }
            if (!swapData) {
                break;
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    public static void main(String[] args) {
        int[] score = new int[]{12, 89, 99, 56, 77, 45, 80, 61, 0};

        bubbleSort(score);

        for (int i = 0; i < score.length; i++) {
            System.out.println(score[i]);
        }
    }
}
